package cn.tedu.egmvc6.controller;

import cn.tedu.egmvc6.mapper.UserMapper;
import cn.tedu.egmvc6.pojo.dto.UserAddParam;
import cn.tedu.egmvc6.pojo.dto.UserUpdateParam;
import cn.tedu.egmvc6.pojo.entity.User;
import cn.tedu.egmvc6.pojo.vo.UserListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("add")
    public String addUser(UserAddParam userAddParam){
        User user = new User();
        BeanUtils.copyProperties(userAddParam,user);
        user.setCreateTime(new Date());
        userMapper.insertUser(user);
        return "添加用户成功";
    }
    @GetMapping("list")
    public List<UserListVO> selectUser(){
        return userMapper.selectUser();
    }

    @PostMapping("del")
    public String deleteUser(Integer id){
        userMapper.deleteUser(id);
        return "删除成功";
    }

    @PostMapping("update")
    public String updateUser(UserUpdateParam userUpdateParam){
        userMapper.updateUser(userUpdateParam);
        return "修改成功";
    }
}
